﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using HugeFlow.OOB.Behaviors;

namespace HugeFlow.OOB.Controls
{
    [TemplatePart(Name = InstallScreen.InstallButtonName, Type = typeof(Button))]
    public class InstallScreen : Control
    {

        #region Members

        internal const string InstallButtonName = "InstallButton";
        internal Button InstallButton;

        #region InstallButtonCaption

        /// <summary> 
        /// Gets or sets the InstallButtonCaption possible Value of the string object.
        /// </summary> 
        [Category("Common Properties"), Description("Caption for InstallButton")]
        public string InstallButtonCaption
        {
            get { return (string)GetValue(InstallButtonCaptionProperty); }
            set { SetValue(InstallButtonCaptionProperty, value); }
        }

        /// <summary> 
        /// Identifies the InstallButtonCaption dependency property.
        /// </summary> 
        public static readonly DependencyProperty InstallButtonCaptionProperty =
                    DependencyProperty.Register(
                          "InstallButtonCaption",
                          typeof(string),
                          typeof(InstallScreen),
                          new PropertyMetadata("Install OOB Application", OnInstallButtonCaptionPropertyChanged));

        /// <summary>
        /// InstallButtonCaptionProperty property changed handler. 
        /// </summary>
        /// <param name="d">InstallScreen that changed its InstallButtonCaption.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnInstallButtonCaptionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            InstallScreen _installScreen = d as InstallScreen;
            if (_installScreen != null)
            {
            }
        }
        #endregion InstallButtonCaption


        #region InstalledMessage

        /// <summary> 
        /// Gets or sets the InstalledMessage possible Value of the string object.
        /// </summary> 
        [Category("Common Properties"), Description("Message when it's not installed")]
        public string InstalledMessage
        {
            get { return (string)GetValue(InstalledMessageProperty); }
            set { SetValue(InstalledMessageProperty, value); }
        }

        /// <summary> 
        /// Identifies the InstalledMessage dependency property.
        /// </summary> 
        public static readonly DependencyProperty InstalledMessageProperty =
                    DependencyProperty.Register(
                          "InstalledMessage",
                          typeof(string),
                          typeof(InstallScreen),
                          new PropertyMetadata("Application installed successfully.\nYou can now close this window.", OnInstalledMessagePropertyChanged));

        /// <summary>
        /// InstalledMessageProperty property changed handler. 
        /// </summary>
        /// <param name="d">InstallScreen that changed its InstalledMessage.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnInstalledMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            InstallScreen _InstallScreen = d as InstallScreen;
            if (_InstallScreen != null)
            {
                //TODO: Handle new value. 
            }
        }
        #endregion InstalledMessage


        #endregion Members

        #region Constructors

        /// <summary>
        /// 기본생성자
        /// </summary>
        public InstallScreen()
        {
            this.DefaultStyleKey = typeof(InstallScreen);

            if (Application.Current.IsRunningOutOfBrowser == true &&
                DesignerProperties.IsInDesignTool == false)
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
            }
            else // OOB 상태아니면
            {
                Application.Current.InstallStateChanged += new EventHandler(Current_InstallStateChanged);
            }
        }

        void Current_InstallStateChanged(object sender, EventArgs e)
        {
            UpdateUIByInstallState();
        }

        #endregion Constructors


        #region Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // 설치 버튼
            InstallButton = this.GetTemplateChild(InstallScreen.InstallButtonName) as Button;
            if (InstallButton != null)
            {
                OOBIntallBehavior installBehavior = new OOBIntallBehavior();
                installBehavior.Attach(InstallButton);
            }

            UpdateUIByInstallState();
        }

        /// <summary>
        /// 인스톨 상태에 따라 UI를 업데이트 함
        /// </summary>
        private void UpdateUIByInstallState()
        {
            switch (Application.Current.InstallState)
            {
                case InstallState.Installed:
                    VisualStateManager.GoToState(this, "Installed", false);
                    break;
                default:
                case InstallState.InstallFailed:
                case InstallState.Installing:
                case InstallState.NotInstalled:
                    VisualStateManager.GoToState(this, "NotInstalled", false);
                    break;
            }
        }

        #endregion Methods

    }
}
